package com.atguigu.tingshu.account.mq;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;


@Component
@Slf4j
public class AccountService {

    @Autowired
    private UserAccountService  userAccountService;
    @KafkaListener(topics = KafkaConstant.QUEUE_USER_REGISTER)
        public void initAccount(ConsumerRecord<String,String> record) {

        String userId = record.value();

        if(!StringUtils.isEmpty(userId)){

            userAccountService.initAccount(Long.parseLong(userId));

        }
            log.info("接收到消息：{}",userId);
        }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minusAccount(ConsumerRecord<String, String> record) {
        String orderNo = record.value();
        if(!StringUtils.isEmpty(orderNo)) {
            userAccountService.minusAccount(orderNo) ;
            log.info("用户账户的总金额扣减完毕了...orderNo: {} "  , orderNo );
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unLock(ConsumerRecord<String, String> record) {
        String orderNo = record.value();
        if(!StringUtils.isEmpty(orderNo)) {
            userAccountService.unLock(orderNo) ;
            log.info("用户账户的锁定金额接收完毕了...orderNo: {} "  , orderNo);
        }
    }

    @KafkaListener(topics = KafkaConstant.QUEUE_RECHARGE_PAY_SUCCESS)
    public void updateRechangreStatus(ConsumerRecord<String, String> record) {
        String value = record.value();
        if(!StringUtils.isEmpty(value)) {
            userAccountService.updateRechangreStatus(value) ;
            log.info("充值记录的订单编号状态更改成功了....orderNo: {} "  , value);
        }
    }

}
